import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["rowKey", "dragSortKey", "dragSortHandlerRender", "onDragSortEnd", "onDataSourceChange", "columns", "dataSource"];
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { MenuOutlined } from '@ant-design/icons';
import { ConfigProvider } from 'antd-v4';
import { useCallback, useContext, useMemo, useRef } from 'react';
import { SortableHandle } from 'react-sortable-hoc';
import ProTable from '../../Table';
import { useDragSort } from '../../utils/useDragSort';
import './index.less'; // 用于创建可拖拽把手组件的工厂

var handleCreator = function handleCreator(handle) {
  return SortableHandle(function () {
    return _jsx(_Fragment, {
      children: handle
    });
  });
};

function DragSortTable(props) {
  var rowKey = props.rowKey,
      dragSortKey = props.dragSortKey,
      dragSortHandlerRender = props.dragSortHandlerRender,
      onDragSortEnd = props.onDragSortEnd,
      onDataSourceChange = props.onDataSourceChange,
      propsColumns = props.columns,
      oriDs = props.dataSource,
      otherProps = _objectWithoutProperties(props, _excluded);

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls; // 默认拖拽把手


  var DragHandle = useMemo(function () {
    return handleCreator(_jsx(MenuOutlined, {
      className: getPrefixCls('pro-table-drag-icon')
    }));
  }, [getPrefixCls]);
  var isDragSortColumn = useCallback(function (item) {
    return item.key === dragSortKey || item.dataIndex === dragSortKey;
  }, [dragSortKey]); // 根据 dragSortKey 查找目标列配置

  var handleColumn = useMemo(function () {
    return propsColumns === null || propsColumns === void 0 ? void 0 : propsColumns.find(function (item) {
      return isDragSortColumn(item);
    });
  }, [propsColumns, isDragSortColumn]); // 记录原始列配置

  var originColumnRef = useRef(_objectSpread({}, handleColumn)); // 使用自定义hooks获取拖拽相关组件的components集合

  var _useDragSort = useDragSort({
    dataSource: oriDs === null || oriDs === void 0 ? void 0 : oriDs.slice(),
    dragSortKey: dragSortKey,
    onDragSortEnd: onDragSortEnd,
    components: props.components,
    rowKey: rowKey
  }),
      components = _useDragSort.components; // 重写列配置的render


  var columns = useMemo(function () {
    var originColumn = originColumnRef.current;
    if (!handleColumn) return propsColumns;

    var dargRender = function dargRender() {
      var _originColumn$render;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var dom = args[0],
          rowData = args[1],
          index = args[2],
          action = args[3],
          schema = args[4];
      var RealHandle = dragSortHandlerRender ? handleCreator(dragSortHandlerRender(rowData, index)) : DragHandle;
      return _jsx("div", {
        className: getPrefixCls('pro-table-drag-visible-cell'),
        children: _jsxs(_Fragment, {
          children: [_jsx(RealHandle, {}), (_originColumn$render = originColumn.render) === null || _originColumn$render === void 0 ? void 0 : _originColumn$render.call(originColumn, dom, rowData, index, action, schema)]
        })
      });
    }; // 重新生成数据


    return propsColumns === null || propsColumns === void 0 ? void 0 : propsColumns.map(function (item) {
      if (!isDragSortColumn(item)) {
        return item;
      }

      return _objectSpread(_objectSpread({}, item), {}, {
        render: dargRender
      });
    });
  }, [DragHandle, dragSortHandlerRender, getPrefixCls, handleColumn, isDragSortColumn, propsColumns]);
  return handleColumn ? _jsx(ProTable, _objectSpread(_objectSpread({}, otherProps), {}, {
    rowKey: rowKey,
    dataSource: oriDs,
    components: components,
    columns: columns,
    onDataSourceChange: onDataSourceChange
  })) :
  /* istanbul ignore next */
  _jsx(ProTable, _objectSpread(_objectSpread({}, otherProps), {}, {
    rowKey: rowKey,
    dataSource: oriDs,
    columns: columns,
    onDataSourceChange: onDataSourceChange
  }));
}

export default DragSortTable;